package icu.xuyijie.springbootlearning2.chapter1_2.controller;

import icu.xuyijie.springbootlearning2.chapter1_2.config.Student;
import icu.xuyijie.springbootlearning2.chapter1_2.config.TestYml;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 徐一杰
 * @date 2025/2/20 14:14
 * @description
 */
@RestController
@RequestMapping("/test")
// 把Student类注入到springboot容器中
//@Import(Student.class)
// 把TestYml类注入到springboot容器中
@EnableConfigurationProperties(TestYml.class)
public class TestController {

    @Autowired
    private Student student;
    @Autowired
    private TestYml testYml;

    @GetMapping("/a")
    // 只有 teacher 角色才能访问此接口
    @PreAuthorize("hasRole('teacher')")
    public void a() {
        System.out.println(student);
    }

    @GetMapping("/b")
    // 有 teacher 或者 student 任意角色，并且有 add 权限才可以访问
    @PreAuthorize("hasAnyRole('student', 'teacher') and hasAuthority('add')")
    public void b() {
        System.out.println(testYml);
    }

    @GetMapping("/c")
    // 有 teacher 或者 student 任意角色，或者有 add 或 delete 任意权限才可以访问
    @PreAuthorize("hasAnyRole('student', 'teacher') or hasAnyAuthority('add', 'delete')")
    public void c() {
        System.out.println(testYml);
    }

}
